# -*- coding: utf-8 -*-
# author: zhanghao
# time: 2023-12-26
# info: 工具类
import os.path

import wget
import urllib.parse


class FileHelper:

    @staticmethod
    def fetchTaskCacheDir():
        current_dir = os.getcwd()
        tmp_dir = current_dir + '/' + 'tmp'
        if not os.path.exists(tmp_dir):
            os.makedirs(tmp_dir)

        return tmp_dir

    @staticmethod
    def fetchFileCacheDir():
        folder_path = FileHelper.fetchTaskCacheDir() + '/file'
        if not os.path.exists(folder_path):
            os.makedirs(folder_path)
        return folder_path

    @staticmethod
    def fetchImageCacheDir():
        folder_path = FileHelper.fetchTaskCacheDir() + '/image'
        if not os.path.exists(folder_path):
            os.makedirs(folder_path)
        return folder_path

    @staticmethod
    def clearTaskCache():
        folder_path = FileHelper.fetchFileCacheDir()
        if not os.path.exists(folder_path):
            os.makedirs(folder_path)
        FileHelper.clearCacheDir(folder_path)

        folder_path = FileHelper.fetchImageCacheDir()
        if not os.path.exists(folder_path):
            os.makedirs(folder_path)
        FileHelper.clearCacheDir(folder_path)

    @staticmethod
    def clearCacheDir(folder_path):
        if os.path.exists(folder_path):
            file_list = os.listdir(folder_path)

            for file_name in file_list:
                file_path = os.path.join(folder_path, file_name)
                # 判断是否为文件
                if os.path.isfile(file_path):
                    # 删除文件
                    os.remove(file_path)

    @staticmethod
    def downloadFile(file_url):
        if file_url is None:
            return ''

        decoded_url = urllib.parse.unquote(file_url)
        folder_path = FileHelper.fetchFileCacheDir()
        wget.download(decoded_url, folder_path)
        file_list = os.listdir(folder_path)
        download_file = ''
        for file_name in file_list:
            download_file = os.path.join(folder_path, file_name)
            break

        return download_file

    @staticmethod
    def fetchDirFile(base_dir):
        files = [os.path.join(base_dir, file) for file in os.listdir(base_dir)]

        deal_files = []
        for file in files:
            if os.path.isfile(file):
                if not FileHelper.isHiddenFile(file):
                    deal_files.append(file)
            elif os.path.isdir(file):
                deap_dir_files = FileHelper.fetchDirFile(file)
                for deap_file in deap_dir_files:
                    deal_files.append(deap_file)

        return deal_files

    @staticmethod
    def isHiddenFile(base_file):
        file_name = os.path.basename(base_file)
        if file_name.startswith('.'):
            return True
        else:
            return False

    @staticmethod
    def getFileType(file_dir):
        file_type = ''
        if file_dir:
            file_type = os.path.splitext(file_dir)[1]
        return file_type

    @staticmethod
    def fetchFileType(file_dir):
        if file_dir:
            file_type = os.path.splitext(file_dir)[1]
            if file_type.lower() == '.pdf':
                return 1
            elif file_type.lower() in ['.png', '.jpg', '.jpeg']:
                return 2
            else:
                return 0
        else:
            return 0

    @staticmethod
    def fetchFileName(file_path):
        file_name = os.path.basename(file_path)
        return file_name

    @staticmethod
    def fetchDirName(file_path):
        dir_name = os.path.dirname(file_path)
        return dir_name

    @staticmethod
    def versionCompare(version1, version2):
        if version1 and version2 and float(version1) > float(version2):
            return True
        else:
            return True
